If someone were to ask you what color an apple is, you would look at the apple and tell them what color you saw. If they then asked you which smelled better, roses or lillies, you would use your nose to smell both of them. Humans get information around their surroundings through our five senses, sight, hearing, touch, smell, and taste. Similarly, robots and computers can collect information by using sensors. Sensors are the wardrobe between real life and the digital world. Whenever your favorite singer records a new album, she has to record it using a microphone, which is a sensor that can detect and record sound. When producers record the latest action movie, they use video cameras, which is a sensor that can detect light.
With the class or in a group, discuss what other types of sensors a computer might use to get information.
There are two main types of sensors, digital and analog. A digital sensor is one that can record whether a signal is on or off, for example a button. On the other hand, an analog sensor is one that can record a spectrum of values, or possibly infinitly many. One example is a temperature sensor. If a temperature sensor can record values from $0 ^\circ$C to $30^\circ$C. One possible value it can record is $10^\circ$C, or $15^\circ$C, or $14.12^\circ$C, or $3.141592^\circ$C. If I were to keep on listing numbers, it would be impossible for me to list every single possible value that the sensor could record!
With the class, for each sensor that you listed in the exercise above, indicate whether it is digital or analog.
Here we will introduce you to python, a high level language that will allow us to send commands to the Raspberry Pi and PSoC.
Firstly, python can be used as a calculator, and can be used to easily do arithmetic. In the code blocks (blocks with In[] and are shaded gray), you can write python code and run it by pressing "shift + enter. In the example below, see how you can perform simple operations. Remember to press "shift + enter" or the "run" button in the toolbar.
In [ ]:
print 40 + 2
In [ ]:
print 7*6
In [ ]:
print 67-25
In [ ]:
print 798/19
We use the print
statement in order to indicate that we want to display the answer to the screen. In the box below, write code to find the answers to the following.
In [ ]:
#Write your code here
#Solution
print 5719*35914
print 7491484+49144192
print 7028004/17
print 4910441-104313
We can also use the print system to print letters and words, such as the example below. In order to print
a word, the word has to be surrounded by a pair of quotation marks (" ")
In [ ]:
print "Hi, I Love Python"
Write some code below to display "Hi, my name is (your name here)!".
In [1]:
#Write your code here
#Solution
print "Hi, my name is Nom!"
One important concept/ability that programming can do is to remember past values using variables. We can them to remember past values, then call on them later on when we need their values. For example, the example below stores a name, then prints it out.
In [ ]:
name = "Billy"
print name
However, if we then reassign the variable later on, it will remember the latest assignment.
In [ ]:
name = "Bobby"
name = "Bambi"
print name
Now we will see how we can use python to read sensors with the Raspberry pi. If you are unfamiliar with the raspberry pi please read the getting started with raspberry pi lesson here.
Once you have set up the PSoC board with the raspberry pi, follow instructions on how to connect the button with the pi.
In [ ]:
#import python library with buttonPressed() method
#b = new Button()
Remember how before we dealt with numbers such as 1,49,3 in the calculations section. Those are a class of values called integers, and the words, such as "Bambi", "Bobby", "Hi", are values known as strings. Here we will introduce another type of values called booleans. Simply put, booleans are either true or false and are denoted as "True" and "False". We can print them like we did with integers and strings.
In [ ]:
yes = True
no = False
print yes
print no
Booleans are useful because they are needed for control flow statements. Control flow statements specifies the order individual statements should be made. The one covered here is the if/else structure. An example is shown below.
In [ ]:
if(True):
print "THIS IS TRUE"
else:
print "THIS IS FALSE"
The first line in the program asks whether or not the statement in the parenthesis is True, or False. Since it is true, it runs the lines following the if statement, thus it prints "THIS IS TRUE". Compare it to the example below.
In [ ]:
if(False):
print "THIS IS TRUE"
else:
print "THIS IS FALSE"
In the example above, the boolean value in the "if" statement's parenthesis is False, so it skips over it and runs the statement in code following the "else" block. If statements don't have just take in True or False, but also variables that contain boolean values. See the example below.
In [ ]:
like_candy = True
if(like_candy):
print "You like candy"
else:
print "You do not like candy"
We set the variable "like_candy" to True and then pass it in the if statement. Since like_candy is true, we run the code following the if block.
Now we will see how we can use these tools to write a simple program that can interface with sensors. A button is a digital sensor, and one way we can see it is to call b.buttonPressed(). This is pretty complicated syntax, so don't worry about it for now. Just know that b.buttonPressed() returns a boolean value, corresponding to whether the button is pressed or not. If the button is pressed, b.buttonPressed() is True, if the button is not presesd, it is false.
Do not press the button and run the following code.
In [ ]:
#print b.buttonPressed()
Now press the button and run the code above again (just click on it and do "shift + enter").
Predict what the following code would do
if(b.buttonPressed()):
print "I AM IM-PRESSED"
else:
print "I AM NOT IM-PRESSED"
Write some code below that prints your name if the button is pressed, and your friends name if it is not pressed.
In [ ]:
#code here
Congratulations! You've learned how to read data from a sensor and a program that behaves in different ways - whether the button is pressed or not.
In [ ]: